package hn.cch.spring_boot_security_rest.controller;


import hn.cch.spring_boot_security_rest.annotation.ApiAnnotation;
import hn.cch.spring_boot_security_rest.message.Response;
import hn.cch.spring_boot_security_rest.request.ApiRequest;
import hn.cch.spring_boot_security_rest.response.ApiResponse;
import hn.cch.util.JsonUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * API接口
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    private static Logger logger = LoggerFactory.getLogger(ApiController.class);


    @Secured("hasAnyRole('system')")
    @GetMapping("getAllApi")
    public Response<ApiResponse> getAllApi() {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setApiUrl("/api/getAllApi");
        apiResponse.setApiName("获取所有API接口");
        Response response = Response.success(apiResponse);
        return response;
    }

    @PreAuthorize("hasAnyRole('system1')")
    @GetMapping("getOwnApi")
    public Response<ApiResponse> getOwnApi() {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setApiUrl("/api/getOwnApi");
        apiResponse.setApiName("获取所属API接口");
        Response response = Response.success(apiResponse);
        return response;
    }

    @ApiAnnotation
    @GetMapping("getAnyApi")
    public Response<ApiResponse> getAnyApi(@ApiParam("查询参数")@RequestParam("query") String query) {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setApiUrl("/api/getAnyApi");
        apiResponse.setApiName("条件查询API接口：" + "\n\t查询参数:" + query);
        Response response = Response.success(apiResponse);
        return response;

    }


    @ApiAnnotation
    @GetMapping("getOneApi")
    @ApiOperation(value = "获取单个API接口")
    public Response<ApiResponse> getOneApi(@ApiParam("请求参数") @RequestParam(required = false) String param,
                                           @ApiParam("路径参数") @PathVariable String path,
                                           @ApiParam("header参数") @RequestHeader String header,
                                           @ApiParam("cookie参数") @CookieValue String cookie) {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setApiUrl("/api/getOneApi");
        apiResponse.setApiName("获取单个API接口" +
                "\n\t请求参数:" + param +
                "\n\t路径参数:" + path +
                "\n\theader参数:" + header +
                "\n\tcookie参数:" + cookie);
        return Response.success(apiResponse);
    }


    @ApiAnnotation
    @PostMapping("setOneApi")
    public Response<ApiResponse> setOneApi(@ApiParam("get参数") @RequestParam String parameter,
                                           @ApiParam("post参数") @RequestBody ApiRequest apiRequest) {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setApiUrl("/api/getOneApi");
        apiResponse.setApiName("设置单个API接口:" +
                "\n\tget参数:" + parameter + "\n\tpost参数" + JsonUtil.toStringJackson(apiRequest));
        return Response.success(apiResponse);
    }

}
